<?php

jimport('joomla.application.component.controller');

class bgController extends JController {
    public function __construct(){
        parent::__construct();
          
        $this->model = $this->getModel();
    }    
    public function display($cachable = false, $urlparams = false) {
        parent::display($cachable, $urlparams);
    }
    
    /**
	* Delete information QUES and ANSQ
	*/
    public function remove(){
        $cid = JRequest::getVar('cid');
        if($this->model->remove($cid)){
            /* remove ansq */
            $this->model->removeansq($cid);
            
            $this->setRedirect('index.php?option=com_question');
        }else{
            //JError::raiseError(500, 'Error ! Can\'t delete' );
            $error = "エラー！データが削除されません。";
            JRequest::setVar('task','');
            JFactory::getApplication()->enqueueMessage($error, 'error');
        }
    }
    
    /**
	* Load information PAGQ at select SERQ
	*/
    public function selectSERQ(){
        $serq_code = JRequest::getCmd('SERQ_CODE',0);
        $model = $this->getModel();
        $pagq = $model->getPAGQ_FULLDOWN($serq_code);
        /*echo '<select id="pagq" name="jform[QUES_PAGQ_CODE]" class="big">
                '; */                          
        foreach($pagq as $value){                       
            echo '<option value="'.$value->PAGQ_CODE.'">'.$value->PAGQ_TITLE.'</option>';       
        }                       
       /* echo '</select>';*/
        $app = &JFactory::getApplication();
        $app->close();
        exit();
    }
    
    /**
	*Save information QUES and ANSQ
	*/
    public function save(){
        // get data form
        $ques = &JRequest::getVar('jform');
        if($this->getModel()->checkExistID($ques['QUES_CODE'])){
            $addNew = false;
        }
        else{
            $addNew = true;
        }
        if(empty($ques['QUES_TITLE'])){
                    $error = "質問を入力してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
        }elseif($ques['QUES_SERQ_CODE']=="0"){
                    $error = "アンケート名を選択してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
        }elseif($ques['QUES_PAGQ_CODE']=="0"){
                    $error = "ページ名を入力してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
        }elseif($ques['QUES_REQUIRED']==""){
                    $error = "必須を入力してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
        }elseif($ques['QUES_TYPE']=="0"){
                    $error = "タイプを選択してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
        }else{ 
            $str = trim($ques['ANSQ_VALUE']);
            $str = str_replace(array("\r\n", "\r", "\n"), "<br />", trim($ques['ANSQ_VALUE'])); 
            $ansq = explode("<br />",$str);
            if($this->model->insertQues($ques,$ansq,$addNew)){
                $this->setRedirect('index.php?option=com_question');
            }
            else{
                $error = "エラー！データが記録されません。";
                JRequest::setVar('task','');
                JFactory::getApplication()->enqueueMessage($error, 'error');
            }
        }
        parent::display();
    }
}